package com.dianganjiao.lib.util

import android.content.Context
import android.view.View
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.ImageViewerPopupView
import com.lxj.xpopup.interfaces.OnSrcViewUpdateListener
import com.lxj.xpopup.interfaces.XPopupImageLoader
import java.io.File

/**
 * create file time : 2021/3/15
 * create user : wxl
 * subscribe :
 */

class ImageViewer {

    companion object {
        fun showResources(
            srcView: ImageView,
            position: Int,
            datas: List<Any>,
            swp: OnSrcViewUpdateListener
        ) {
            if (srcView.context is AppCompatActivity) {
                XPopup.Builder(srcView.context).asImageViewer(srcView, position, datas,
                    false, false, -1, -1, -1, false,
                    swp
                    , object : XPopupImageLoader {
                        override fun loadImage(position: Int, uri: Any, imageView: ImageView) {
                            ImgLoader.loadResouce(imageView, uri as Int)
                        }

                        override fun getImageFile(context: Context, uri: Any): File? {
                            return FileCommon.getExternalImageFile()
                        }

                    }
                ).toggle()
            }
        }


        fun showResources(
            srcView: View,
            imgSrcId: Int,
            position: Int,
            datas: List<Any>
        ) {
            if (srcView.context is AppCompatActivity) {
                XPopup.Builder(srcView.context)
                    .asImageViewer(srcView.findViewById(imgSrcId), position, datas,
                        false, false, -1, -1, -1, false,
                        { popupView, position ->
                            popupView.updateSrcView(
                                srcView.findViewById(
                                    imgSrcId
                                )
                            )
                        }
                        , object : XPopupImageLoader {
                            override fun loadImage(position: Int, uri: Any, imageView: ImageView) {
                                ImgLoader.loadResouce(imageView, uri as Int)
                            }

                            override fun getImageFile(context: Context, uri: Any): File? {
                                return FileCommon.getExternalImageFile()
                            }

                        }
                    ).toggle()
            }
        }


        fun show(
            srcView: View,
            imgSrcId: Int,
            position: Int,
            datas: List<Any>
        ) {
            if (srcView.context is AppCompatActivity) {
                XPopup.Builder(srcView.context)
                    .asImageViewer(srcView.findViewById(imgSrcId), position, datas,
                        false, false, -1, -1, -1, false,
                        { popupView, position ->
                            popupView.updateSrcView(
                                srcView.findViewById(
                                    imgSrcId
                                )
                            )
                        }
                        , object : XPopupImageLoader {
                            override fun loadImage(position: Int, uri: Any, imageView: ImageView) {
                                ImgLoader.loadResouce(imageView, uri as Int)
                            }

                            override fun getImageFile(context: Context, uri: Any): File? {
                                return FileCommon.getExternalImageFile()
                            }

                        }
                    ).toggle()
            }
        }

        fun show(context: Context,position: Int,urlList:List<String>){
            XPopup.Builder(context)
                .asImageViewer(null,position,urlList,object : OnSrcViewUpdateListener{
                    override fun onSrcViewUpdate(popupView: ImageViewerPopupView, position: Int) {

                    }
                },object : XPopupImageLoader{
                    override fun loadImage(position: Int, uri: Any, imageView: ImageView) {
                        ImgLoader.load(imageView,uri as String)
                    }

                    override fun getImageFile(context: Context, uri: Any): File? {
                        return FileCommon.getExternalImageFile()
                    }
                }).toggle()
        }

        fun showUrl(context: Context,position: Int,urlList:List<String>){
            XPopup.Builder(context)
                .asImageViewer(null,position,urlList,object : OnSrcViewUpdateListener{
                    override fun onSrcViewUpdate(popupView: ImageViewerPopupView, position: Int) {

                    }
                },object : XPopupImageLoader{
                    override fun loadImage(position: Int, uri: Any, imageView: ImageView) {
                        ImgLoader.load(imageView,uri as String)
                    }

                    override fun getImageFile(context: Context, uri: Any): File? {
                        return FileCommon.getExternalImageFile()
                    }
                }).toggle()
        }
    }
}